package cn.doctor.assistant.pad.ui.hudong;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ImageView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.doctor.assistant.pad.MyApplication;
import cn.doctor.assistant.pad.R;
import cn.doctor.assistant.pad.api.hudong.ChatApi;
import cn.doctor.assistant.pad.base.BaseFragment;
import cn.doctor.assistant.pad.base.ResultCallback;
import cn.doctor.assistant.pad.entity.ContactInfo;
import cn.doctor.assistant.pad.entity.GroupsBean;

/**
 * Created by jiaolf on 2016/3/28.
 * 互动-联系人-列表
 */
public class ContactsListFragment extends BaseFragment implements
        SwipeRefreshLayout.OnRefreshListener,
        ExpandableListView.OnChildClickListener,
        ExpandableListView.OnGroupClickListener {

    View rootView;
    @InjectView(R.id.friendsListView)
    ExpandableListView friendsListView;
    @InjectView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;
    // 好友分组和对应的好友列表
    List<GroupsBean> groups;

    GroupListAdapter mGroupAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_chat_contacts_list, container, false);
        ButterKnife.inject(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initialize();
    }

    private void initialize() {
        swipeRefreshLayout.setColorSchemeColors(R.color.assistant_theme_color, R.color.colorPrimaryDark);
        swipeRefreshLayout.setOnRefreshListener(this);

        friendsListView.setGroupIndicator(null);
        friendsListView.setVerticalScrollBarEnabled(false);
        friendsListView.setOnChildClickListener(this);
        friendsListView.setOnGroupClickListener(this);

        if (groups == null || groups.isEmpty()) {
            getContacts();
        }
    }

    private void getContacts() {
        showProgress("正在加载联系人列表...");
        ChatApi.getContactGroupsByType(new ResultCallback<List<GroupsBean>>() {
            @Override
            public void onSuccess(List<GroupsBean> result) {
                stopProgress();
                swipeRefreshLayout.setRefreshing(false);
                if (result != null) {
                    MyApplication.getInstance().setGroups(result);
                    groups = result;
                    if (mGroupAdapter == null) {
                        mGroupAdapter = new GroupListAdapter(activity, groups);
                        friendsListView.setAdapter(mGroupAdapter);
                    } else {
                        mGroupAdapter.notifyDataSetChanged();
                    }

                    // 找到第一个联系人
                    ContactInfo user;
                    for (int i = 0; i < groups.size(); i++) {
                        List<ContactInfo> users = groups.get(i).getUsers();
                        if(users == null || users.isEmpty()) continue;
                        for (int j = 0; j < users.size(); j++) {
                            user = users.get(j);
                            MyApplication.getInstance().onSelectedContactChanged(user);
                            return;
                        }
                    }

                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void onRefresh() {
        getContacts();
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        ContactInfo contactInfo = (ContactInfo) mGroupAdapter.getChild(groupPosition, childPosition);
        MyApplication.getInstance().onSelectedContactChanged(contactInfo);
        return false;
    }

    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
        ImageView arrar = (ImageView) v.findViewById(R.id.iv_arrar);
        if (friendsListView.isGroupExpanded(groupPosition)) {
            arrar.setImageResource(R.drawable.peer_left);
        } else {
            arrar.setImageResource(R.drawable.peer_down);
        }
        return false;
    }
}
